package cn.shyouchi.wuguanjia.android.ui.home.security.thread.me;

import android.app.Activity;
import android.content.Intent;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.UserStorageProvider;
import cn.shyouchi.wuguanjia.android.api.profile.ProfileApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import io.realm.Realm;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class FilloutProfileActivity extends BaseActivity implements View.OnClickListener {

    ProfileApi mProfileApi;
    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_field_name)
    TextView mTvFieldName;
    @BindView(R.id.edit_field_value)
    EditText mEditFieldValue;
    @BindView(R.id.btn_save)
    Button mBtnSave;

    String title;
    String content;
    boolean singleLine;
    int inputtype;
    int type;


    @Override
    public int initContentView() {
        return R.layout.activity_fillout_profile;
    }

    @Override
    public void initInjector() {
        mProfileApi = new ProfileApi(mContext);
        title = getIntent().getStringExtra("title");
        type = getIntent().getIntExtra("type", 0);
        content = getIntent().getStringExtra("content");
        singleLine = getIntent().getBooleanExtra("single", false);
        inputtype = getIntent().getIntExtra("inputtype", InputType.TYPE_CLASS_NUMBER);
    }


    public static void start(Activity activity, String title, String content, int type, boolean singleLine, int inputtype) {
        activity.startActivityForResult(new Intent(activity, FilloutProfileActivity.class)
                .putExtra("title", title)
                .putExtra("type", type)
                .putExtra("content", content)
                .putExtra("inputtype", inputtype)
                .putExtra("single", singleLine), 1001);
    }


    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(title);
        setSupportActionBar(mTopBar.getToolbar());
        mBtnSave.setOnClickListener(this);
        mTvFieldName.setText(title);
        mEditFieldValue.setInputType(inputtype);
        mEditFieldValue.setText(content);
        if (singleLine) {
            mEditFieldValue.setMaxLines(1);
            mEditFieldValue.setMinLines(1);
        } else {
            mEditFieldValue.setMinLines(9);
        }

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void saveChange(View v) {
        Map<String, Object> params = new HashMap<>();
        String field = "";

        if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_ADDR) {
            field = "living_address";
        } else if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_JINGYAN) {
            field = "work_experience";
        } else if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_NEIRONG) {
            field = "work_content";
        } else if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_COM) {
            field = "employer";
        } else if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_BAND) {
            field = "bank_card";
        } else if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_SECURITY) {
            field = "security_card";
        }

        params.put(field, String.valueOf(mEditFieldValue.getText().toString()));
        mProfileApi.upProfile(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult baseRequestResult) {
                        Realm.getDefaultInstance()
                                .executeTransaction(new Realm.Transaction() {
                                    @Override
                                    public void execute(Realm realm) {
                                        if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_ADDR) {
                                            UserStorageProvider.Default().getUserData().setLiving_address(mEditFieldValue.getText().toString());

                                        } else if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_JINGYAN) {
                                            UserStorageProvider.Default().getUserData().setWork_experience(mEditFieldValue.getText().toString());

                                        } else if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_NEIRONG) {
                                            UserStorageProvider.Default().getUserData().setWork_content(mEditFieldValue.getText().toString());

                                        } else if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_COM) {
                                            UserStorageProvider.Default().getUserData().setEmployer(mEditFieldValue.getText().toString());

                                        } else if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_BAND) {
                                            UserStorageProvider.Default().getUserData().setBank_card(mEditFieldValue.getText().toString());

                                        } else if (type == ProfileUpdateActivity.UPDATE_PROFILE_FIELD_SECURITY) {
                                            UserStorageProvider.Default().getUserData().setSecurity_card(mEditFieldValue.getText().toString());

                                        }
                                        setResult(1000);
                                        finish();
                                    }
                                });


                    }
                });
    }


    @Override
    public void onClick(View v) {
        saveChange(v);
    }





    private void zipField() {



    }




}
